package com.yun.cloud.controller;

import com.yun.cloud.entity.Dept;
import com.yun.cloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by lsy
 * on 2018/6/8 11:54.
 */
@RestController
public class DeptController {

    @Autowired
    private DeptService service;
    @Autowired
    private DiscoveryClient client;


    @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
    public boolean add(@RequestBody Dept dept) {
        return service.add(dept);
    }


    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    public Dept get(@PathVariable("id") Long id) {
        return service.get(id);
    }

    @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
    public List<Dept> list() {

        return service.list();
    }

    @Value("${server.port}")
    private String serverPort;

    @RequestMapping(value = "/dept/gg", method = RequestMethod.GET)
    public String gg() {

        return "i am from port" + serverPort;
    }

    @RequestMapping("/dept/discovery")
    public Object discovery() {
        List<String> services = client.getServices();
        System.out.println("*********" + services);

        List<ServiceInstance> instances = client.getInstances("MICRO-SERVICE-CLOUD-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }
        return this.client;
    }
}